package com.pwt.utils;

import com.pwt.common.CustomException;
import com.pwt.entity.AcceptanceHand;
import com.pwt.entity.AcceptanceLimit;
import com.pwt.entity.MERKMALCONTROL;
import com.pwt.enu.AcceptanceEnum;
import com.pwt.model.ChartModel;
import com.pwt.model.crustallus.FeatureModel;
import com.pwt.repository.AcceptanceHandRepository;
import com.pwt.repository.AcceptanceLimitRepository;
import com.pwt.repository.MERKMALCONTROLRepository;
import com.pwt.repository.WERTEVARRepository;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dw
 * @version 1.0.0
 * @date 2021/9/26
 */
@Component
public class SigmaUtil {

    public AcceptanceLimitRepository acceptanceLimitRepository;

    private AcceptanceHandRepository acceptanceHandRepository;

    private WERTEVARRepository wertevarRepository;

    private MERKMALCONTROLRepository merkmalcontrolRepository;


    public SigmaUtil(AcceptanceLimitRepository acceptanceLimitRepository, AcceptanceHandRepository acceptanceHandRepository, WERTEVARRepository wertevarRepository, MERKMALCONTROLRepository merkmalcontrolRepository) {
        this.acceptanceLimitRepository = acceptanceLimitRepository;
        this.acceptanceHandRepository = acceptanceHandRepository;
        this.wertevarRepository = wertevarRepository;
        this.merkmalcontrolRepository = merkmalcontrolRepository;
    }

    public static Double  getSigma(List<Double> list, Double avg){
        Double sigma = 0D;
        if(list == null || list.size() < 1){
            return sigma;
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i) != null){
                sigma = sigma +Math.pow((list.get(i) - avg),2) ;
            }
        }
        if(sigma == 0.00 ){
            return sigma;
        }
        sigma = Math.sqrt(MathUtil.divDouble(sigma,list.size()*1.00));

        return DoubleUtils.retain2(sigma);
    }


    public static Double avg(List<Double> list){
        if(list == null || list.size() < 1){
            return 0D;
        }
        Double avg = 0D;
        for ( Double d:list) {
            if(d != null){
                avg = avg+d;
            }
        }
        return DoubleUtils.retain2(avg/list.size());
    }

    public ChartModel getAcceptanceLimit(Integer k1000 , Short k2000) throws CustomException {
        ChartModel chartModel = new ChartModel();
        //被测参数 数据查询
        FeatureModel featureModel = this.wertevarRepository.getCharacteristics(k1000,k2000);
        chartModel.setPart(featureModel.getPart());
        chartModel.setPartId(featureModel.getPartId());
        if(featureModel == null){
            throw new CustomException("未查到被测参数！");
        }
        //名义值
        Double nominal = featureModel.getK2101();

        //报废限
        chartModel.setScrapDown(featureModel.getK2114());
        chartModel.setScrapUp(featureModel.getK2115());
        //上下限
        chartModel.setToleranceDown(featureModel.getK2110());
        chartModel.setToleranceUp(featureModel.getK2111());
//        AcceptanceHand acceptanceHand = this.acceptanceHandRepository.getAcceptanceHandByTeilAndMerkmal(k1000, k2000);
//        if(acceptanceHand != null){
//            chartModel.setAcceptanceUp(acceptanceHand.getMeogw());
//            chartModel.setAcceptanceDown(acceptanceHand.getMeugw());
//        }else{
//            AcceptanceLimit acceptanceLimit= this.acceptanceLimitRepository.queryByTeilAndMerkmal(k1000,k2000);
//            List<Double>  list = null;
//            Double sigma = 0.00D;
//            if(acceptanceLimit != null){
//                if(acceptanceLimit.getType() == AcceptanceEnum.ALL.getType()){
//                    Integer value = acceptanceLimit.getValue() == null ? 1 : acceptanceLimit.getValue();
//                    list = this.wertevarRepository.getMeasureValueOnMonth(k1000,k2000,value);
//                }
//                if(acceptanceLimit.getType() == AcceptanceEnum.HISTORY_FOR_125.getType()){
//                    Integer value = acceptanceLimit.getValue() == null ? 125 : acceptanceLimit.getValue();
//                    list = this.wertevarRepository.getMeasureValueOnTop(value,k1000,k2000);
//                }
//            }else{
//                list = this.wertevarRepository.getMeasureValueOnTop(125,k1000,k2000);
//            }
//            if(list != null){
//                //平均值
//                Double avg = avg(list);
//                if(nominal == null){
//                    nominal = avg;
//                }
//                sigma = getSigma(list,avg);
//                MERKMALCONTROL control = merkmalcontrolRepository.queryMERKMALCONTROLByMETEILAndMEMERKMAL(k1000,k2000);
//                if(control != null){
//                    chartModel.setAcceptanceUp(DoubleUtils.retain2(control.getMEOGWCONTROL()));
//                    chartModel.setAcceptanceDown(DoubleUtils.retain2(control.getMEUGWCONTROL()));
//                }
//                chartModel.setAcceptanceUp(DoubleUtils.retain2(nominal+3*sigma));
//                chartModel.setAcceptanceDown(DoubleUtils.retain2(nominal-3*sigma));
//            }
//        }
        return chartModel;
    }

}
